package org.chenzhi.mvp;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

/**
 * @author orange
 */

public class SimpleMVP {

    private static SimpleMVP instance;

    private Activity topActivity; //栈顶activity
    private Application app;

    private Application.ActivityLifecycleCallbacks callbacks =
            new Application.ActivityLifecycleCallbacks() {
                @Override
                public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                    topActivity = activity;
                }

                @Override
                public void onActivityStarted(Activity activity) {
                    topActivity = activity;
                }

                @Override
                public void onActivityResumed(Activity activity) {
                    topActivity = activity;
                }

                @Override
                public void onActivityPaused(Activity activity) {

                }

                @Override
                public void onActivityStopped(Activity activity) {

                }

                @Override
                public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

                }

                @Override
                public void onActivityDestroyed(Activity activity) {

                }
            };

    public static SimpleMVP getInstance() {
        if (instance == null) {
            synchronized (SimpleMVP.class) {
                if (instance == null) {
                    instance = new SimpleMVP();
                }
            }
        }

        return instance;
    }

    public void install(Application app) {
        if (app == null) {
            throw new NullPointerException("SimpleMVP can not attach to a null application");
        }

        this.app = app;

        app.registerActivityLifecycleCallbacks(callbacks);
    }

    public boolean isInstalled() {
        return app != null;
    }
}
